package cc.wenshixin.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import antlr.collections.impl.LList;
import cc.wenshixin.entity.Customer;
import cc.wenshixin.entity.LinkMan;
import cc.wenshixin.service.CustomerService;
import cc.wenshixin.service.LinkManService;

public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
	//属性注入
	private LinkManService linkManService;
	public void setLinkManService(LinkManService linkManService) {
		this.linkManService = linkManService;
	}
	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	//模型驱动
	private LinkMan linkMan = new LinkMan();
	@Override
	public LinkMan getModel() {
		return linkMan;
	}
	
	//属性封装
	private File upload;
	private String uploadFileName;
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	//转到添加页面
	public String toAddPage() {
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("customerList", customerList);
		return "toAddPage";
	}
	
	//添加的方法
	public String add() throws IOException {
		if(upload != null) {
			File serverFile = new File("C:\\service\\file"+"/"+uploadFileName);
			FileUtils.copyFile(upload, serverFile);
		}
		linkMan.toString();
		linkManService.add(linkMan);
		return "add";
	}
	
	//显示联系人方法
	public String list() {
		List<LinkMan> linkManList = linkManService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("linkManList", linkManList);
		return "list";
	}
	
	//转到修改页面
	public String toEditPage() {
		int id = linkMan.getId();
		LinkMan linkMan = linkManService.findOne(id);
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("linkMan", linkMan);
		request.setAttribute("customerList", customerList);
		return "toEditPage";
	}
	
	//修改的方法
	public String edit() {
		linkManService.update(linkMan);
		return "edit";
	}
	
	//删除的方法
	public String delete() {
		int id = linkMan.getId();
		LinkMan l = linkManService.findOne(id);
		if(linkMan != null) {
			linkManService.delete(l);
		}
		return "delete";
	}
	
	//转到查询页面
	public String toSelectPage() {
		List<Customer> customerList = customerService.findAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("customerList", customerList);
		return "toSelectPage";
	}
	
	//多条件查询的方法
	public String select() {
		List<LinkMan> linkManList = linkManService.selectMoreCondition(linkMan);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("linkManList", linkManList);
		return "select";
	}
}
